<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="36%"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="参数名称" prop="label_name">
          <el-input v-model="ruleForm.label_name" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数标识" prop="column_name">
          <el-input v-model="ruleForm.column_name" placeholder="请输入参数标识" />
        </el-form-item>
        <el-form-item label="参数类型" prop="type_id">
          <el-cascader
            v-model="ruleForm.type_id"
            :options="options"
            :props="{ expandTrigger: 'hover', value: 'id', label: 'type_name', children: 'childList' }"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="ruleForm.status" :label="1">正常</el-radio>
          <el-radio v-model="ruleForm.status" :label="0">禁止</el-radio>
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm">取消</el-button>
          <el-button type="primary" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { createData, updateData, detailData, dataTypeList } from '@/api/dataType'
export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      show: false,
      options: [],
      ruleForm: {
        label_name: '',
        column_name: '',
        type_id: [],
        status: 1
      },
      rules: {
        label_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        column_name: [
          { required: true, message: '请输入参数标识', trigger: 'blur' }
        ],
        type_id: [
          { required: true, message: '请选择参数类型', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 编辑
    if (this.id) {
      // this.ruleForm.pid = this.id
      if (this.title === '新增') {
        this.ruleForm.pid = this.id
      } else {
        this.getDetailById(this.id)
      }
    }
    this.getOptions()
  },
  methods: {
    // 获取详情
    getDetailById(id) {
      detailData({ id }).then(res => {
        this.ruleForm = res.data
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            //  是编辑
            updateData(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            this.ruleForm.type_id = this.ruleForm.type_id[1]
            createData(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    },
    handleChange(e) {
      console.log(e)
    },
    // 获取参数类型数据
    getOptions() {
      dataTypeList({
        sort: 'created_at',
        offset: 0,
        limit: 50,
        order: 'ascending',
        type_name: '',
        pid: 0
      }).then(res => {
        this.options = res.data.categories
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 10px;
    border: 1px dashed #cccccc;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 10px;
  }
</style>
